﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ColourPicker
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cboType.SelectedIndex = 0;

            Picker();
        }


        private void Picker()
        {
            // Show the color dialog.
            DialogResult result = colorDialog1.ShowDialog();
            // See if user pressed ok.
            if (result == DialogResult.OK)
            {
                // Set form background to the selected color.
                this.BackColor = colorDialog1.Color;

                // Set the text colour of the label
                this.lblType.ForeColor = UIntToColor(0xFFFFFFFFu ^ ColorToUInt(colorDialog1.Color));

                ConvertColour();
            }
        }

        private void ConvertColour()
        {
            switch (cboType.SelectedItem.ToString())
            {
                case "RGB":
                    ConvertRGB();
                    break;
                case "RGBA":
                    ConvertRGBA();
                    break;
            }
        }

        private void ConvertRGBA()
        {
            txtColour.Text = ((float)this.BackColor.R / 255).ToString("0.0###########################") + "f , " +
                             ((float)this.BackColor.G / 255).ToString("0.0###########################") + "f , " +
                             ((float)this.BackColor.B / 255).ToString("0.0###########################") + "f , " +
                             ((float)this.BackColor.A / 255).ToString("0.0###########################") + "f ";

            txtColour.Select();
        }

        private void ConvertRGB()
        {
            txtColour.Text = ((float)this.BackColor.R / 255).ToString("0.0###########################") + "f , " +
                             ((float)this.BackColor.G / 255).ToString("0.0###########################") + "f , " +
                             ((float)this.BackColor.B / 255).ToString("0.0###########################") + "f ";

            txtColour.Select();

        }

        private void PickColor_Click(object sender, EventArgs e)
        {
            Picker();
        }


        private uint ColorToUInt(Color color)
        {
            return (uint)((color.A << 24) | (color.R << 16) |
                          (color.G << 8) | (color.B << 0));
        }

        private Color UIntToColor(uint color)
        {
            byte a = (byte)(color >> 24);
            byte r = (byte)(color >> 16);
            byte g = (byte)(color >> 8);
            byte b = (byte)(color >> 0);
            return Color.FromArgb(a, r, g, b);
        }

        private void cboType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ConvertColour();
        }

       
       
    }
}
